package com.huxun.controller;

import com.huxun.bean.Foodtype;
import com.huxun.service.FoodTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class FoodTypeController {
    @Autowired
    FoodTypeService foodTypeService;
    @RequestMapping("/foodType_querylist")
    public String querylist(Foodtype foodtype, Model model){
        List<Foodtype> foodTypes = foodTypeService.getFoodTypes(foodtype);
        System.out.println("查询到的页面数据是"+foodTypes);
        model.addAttribute("foodTypes",foodTypes);
        //要回显的数据对象
        model.addAttribute("foodtype",foodtype);
        return "foodType";
    }
    @RequestMapping("/foodType_add")
    public void addFoodType(Foodtype foodtype, HttpServletResponse response) throws Exception{
        //解决中文乱码
        response.setContentType("text/html;charset=utf-8");
        int count = foodTypeService.addFoodType(foodtype);
        if (count>0){
            response.getWriter().write("<script>\n" +
                    "\talert(\"新增菜系成功\");\n" +
                    "\tlocation.href=\"/foodType_querylist\"\n" +
                    "</script>");
        }else {
            response.getWriter().write("<script>\n" +
                    "\talert(\"新增菜系失败\");\n" +
                    "\tlocation.href=\"/page_savefoodType\"\n" +
                    "</script>");
        }
    }
    @RequestMapping("/foodType_toupdate/{id}")
    public String toupdate(@PathVariable("id") int id,Model model){
        Foodtype foodtype = foodTypeService.getById(id);
        model.addAttribute("foodtype",foodtype);
        return "updatefoodType";
    }

    @RequestMapping("/foodType_update")
    public void update(Foodtype foodtype,HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        int count = foodTypeService.updateFoodType(foodtype);
        if (count>0){
            response.getWriter().write("<script>\n" +
                    "\talert(\"新增菜系成功\");\n" +
                    "\tlocation.href=\"/foodType_querylist\"\n" +
                    "</script>");
        }else {
            response.getWriter().write("<script>alert('菜系更新失败!');location.href='/foodType_toupdate/"+foodtype.getId()+"';</script>");
        }
    }
    @RequestMapping("/foodType_delete/{id}")
    public void delete(@PathVariable("id")int id,HttpServletResponse response) throws Exception{
        response.setContentType("text/html;charset=utf-8");
        int count = foodTypeService.deleteFoodType(id);
        if (count>0){
            response.getWriter().write("<script>\n" +
                    "\talert(\"菜系删除成功\");\n" +
                    "\tlocation.href=\"/foodType_querylist\"\n" +
                    "</script>");
        }else {
            response.getWriter().write("<script>alert('菜系删除失败!');location.href='/foodType_querylist';</script>");
        }
    }
}
